myApp.controller('asset_detail', function ($rootScope, $scope, $http, $uibModalInstance, httpService, formBoxData) {
	
	/*关闭的图片*/
	$scope.closeImg={'img':'images/close-default.png','defaultImg':'images/close-default.png','downImg':'images/close-btndown.png','hoverImg':'images/close-hover.png'};
    $scope.title = formBoxData.title;
    $scope.formData = formBoxData.checkedAsset;
    $scope.inputObject = {};

	// 获取人员列表
    $scope.getPeopleList = function () {
        $http({
            method: 'GET',
            url: 'json/peopleList.json'
        }).success(function (response) {
            $scope.peopleList = response.data;
        }).error(function (response) {
            // called asynchronously if an error occurs
            // or server returns response with an error status.
        });
    };
    $scope.setFormUser = function () {
        $scope.formData.userId = $scope.inputObject.user.id;
    };

    // 获取管理员列表
    $scope.getManagerList = function () {
        $http({
            method: 'GET',
            url: 'json/managerList.json'
        }).success(function (response) {
            $scope.managerList = response.data;
        }).error(function (response) {
            // called asynchronously if an error occurs
            // or server returns response with an error status.
        });
    };
    $scope.setFormManager = function () {
        $scope.formData.manager = $scope.inputObject.manager.id;
    };

    // 获取资产类型列表
    $scope.getAssetTypeList = function () {
        $http({
            method: 'GET',
            url: 'json/assetTypeList.json'
        }).success(function (response) {
            $scope.assetTypeList = response.data;
            $scope.formData.assetType = undefined;
        }).error(function (response) {
            // called asynchronously if an error occurs
            // or server returns response with an error status.
        });
    };

    function formInit() {
        $scope.getPeopleList();
        $scope.getManagerList();
        $scope.getAssetTypeList();
        $scope.couldEdit = false;
        $scope.btnMsg = "编辑";
    }
    formInit();

    // 日期选择
    $scope.datePopup = {
        opened: false
    };
    $scope.datePopup2 = {
        opened: false
    };
    $scope.datePopup3 = {
        opened: false
    };

    // 选择图片
    $scope.selectImg = function(obj) {
    	console.log($scope.formData.userId);
        $file = $(obj);
        var file = obj.files[0];
        var reader = new FileReader();
        reader.readAsDataURL(file);// 读出 base64
        reader.onload = (function(evt) {
            $scope.assetPicBase64 = evt.target.result;
        });
    };

    // 保存
    $scope.ok = function () {
        if($scope.couldEdit) {
			!$scope.formData.assetCost  ? $scope.form.assetCost.$isTure = true : $scope.form.assetCost.$isTure = false;
			!$scope.formData.buyType  ? $scope.buyType = true : $scope.buyType = false;
			!$scope.formData.buyTime  ? $scope.buyTime = true : $scope.buyTime = false;
			!$scope.formData.modelNumber  ? $scope.form.modelNumber.$isTure = true : $scope.form.modelNumber.$isTure = false;
			!$scope.formData.brand ? $scope.form.brand.$isTure = true : $scope.form.brand.$isTure = false;
			!$scope.formData.useCompany  ? $scope.useCompany = true : $scope.useCompany = false;
			if (
				$scope.formData.assetCost &&
				$scope.formData.buyType &&
				$scope.formData.buyTime &&
				$scope.formData.modelNumber &&
				$scope.formData.brand &&
				$scope.formData.useCompany
			) {
				httpService.postRequest(formBoxData.modalApi, $scope.formData).then(function (res) {
					if (res.resultCode === 0) {
						$rootScope.$emit('to-list', 'moreData');
					}
					$scope.couldEdit = false;
					$scope.btnMsg = "编辑";
				});
			}
		}else{
			$scope.couldEdit = true;
			$scope.btnMsg = "保存";
        }
    };
    // 取消
    $scope.cancel = function () {
        $uibModalInstance.dismiss('cancel');
    };
});
